package com.shuoyue.ycgk.views;

import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.google.android.material.tabs.TabLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.entity.Type;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class TabLayoutUtil<T> {
    ViewHolder holder = null;

    public void setTabTextSizeWhenSelect(TabLayout mTabLayout, List<T> titles) {
        setTabTextSizeWhenSelect(mTabLayout, titles, 0);
    }

    public void setTabTextSizeWhenSelect(TabLayout mTabLayout, List<T> titles, int index) {

        for (int i = 0; i < titles.size(); i++) {
            //获取tab
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            //给tab设置自定义布局
            tab.setCustomView(R.layout.item_tabitem);
            holder = new ViewHolder(tab.getCustomView());
            //填充数据
            holder.title.setText(titles.get(i).toString());
            //默认选择第一项
            if (i == index) {
                holder.title.setSelected(true);
                holder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                holder.title.setTextColor(ContextCompat.getColor(tab.getCustomView().getContext(), R.color.themeColor));
            }
        }

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                holder = new ViewHolder(tab.getCustomView());
                holder.title.setSelected(true);
                //设置选中后的字体大小
                holder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
                holder.title.setTextColor(ContextCompat.getColor(tab.getCustomView().getContext(), R.color.themeColor));

//                holder.indicator.setVisibility(View.VISIBLE);
                //关联Viewpager
//                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                holder = new ViewHolder(tab.getCustomView());
                holder.title.setSelected(false);
                //恢复默认字体大小
                holder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
                holder.title.setTextColor(ContextCompat.getColor(tab.getCustomView().getContext(), R.color.text_sub_title));

//                holder.indicator.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

    class ViewHolder {
        TextView title;
//        View indicator;

        ViewHolder(View tabView) {
            title = (TextView) tabView.findViewById(R.id.title);
//            indicator = tabView.findViewById(R.id.indicator);
        }
    }
}